package com.xiaodong.reggie.currency;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;

@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class ExceptionTackle {

    @ExceptionHandler (SQLIntegrityConstraintViolationException.class)
    public R<String> man(SQLIntegrityConstraintViolationException es){

//        log.info(es.getMessage());
        if(es.getMessage().contains("Duplicate entry")){
            String[] s = es.getMessage().split(" ");
            String s1 = s[2] + "已存在";
            return R.error(s1);
        }

        return R.error("添加失败");

    }


    @ExceptionHandler (CostomException.class)
    public R<String> men(CostomException es){

        log.info(es.getMessage());

        return R.error(es.getMessage());

    }

}
